home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte17 / filttlft.c < prev    next >
C/C++ Source or Header  |  1996-01-31  |  7KB  |  228 lines

  1.  
  2. #include <windows.h>  
  3. #include "FilTTLFT.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. typedef struct
  108. {
  109.    WORD Day:5;     // Bits 0-4
  110.    WORD Month:4;   // Bits 5-8
  111.    WORD Year:7;    // Bits 9-15
  112. } DOSDATE;
  113.  
  114. typedef struct
  115. {
  116.    WORD Second:5;  // Bits 0-4
  117.    WORD Minute:6;  // Bits 5-10
  118.    WORD Hour:5;    // Bits 11-15
  119. } DOSTIME;
  120.  
  121. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  122. {
  123.    switch( uMsg )
  124.    {
  125.       case WM_COMMAND :
  126.               switch( LOWORD( wParam ) )
  127.               {
  128.                  case IDM_TEST :
  129.                         {
  130.                            DOSDATE    Date;
  131.                            DOSTIME    Time;
  132.                            FILETIME   fTime, fLocTime;
  133.                            SYSTEMTIME sysTime;
  134.                            HDC        hDC;
  135.                            HANDLE     hFile;
  136.                            char       szMsg[128];
  137.  
  138.                            hDC = GetDC( hWnd );
  139.  
  140.                            // Open/create a file.
  141.                            //....................
  142.                            hFile = CreateFile( "FILE.DAT", 
  143.                                      GENERIC_READ | GENERIC_WRITE,
  144.                                      0, NULL, OPEN_ALWAYS,
  145.                                      FILE_ATTRIBUTE_NORMAL, NULL );
  146.  
  147.                            // Retrieve the file time and convert it to
  148.                            // local file time.
  149.                            //.........................................
  150.                            GetFileTime( hFile, NULL, NULL, &fTime );
  151.                            FileTimeToLocalFileTime( &fTime, &fLocTime );
  152.  
  153.                            // Convert to DOS date/time.
  154.                            //..........................
  155.                            FileTimeToDosDateTime( &fLocTime, (WORD*)&Date,
  156.                                                   (WORD*)&Time );
  157.  
  158.                            wsprintf( szMsg, 
  159.                                     "DOS Date/Time: %.2d/%.2d/%d  %d:%.2d:%.2d",
  160.                                     Date.Month, Date.Day, Date.Year+1980,
  161.                                     Time.Hour, Time.Minute, Time.Second*2 );
  162.  
  163.                            TextOut( hDC, 10, 10, szMsg, strlen( szMsg ) );
  164.  
  165.                            // Convert UTC time to a SYSTEMTIME structure.
  166.                            //............................................
  167.                            FileTimeToSystemTime( &fTime, &sysTime );
  168.  
  169.                            wsprintf( szMsg, 
  170.                                     "System (UTC) Date/Time: %.2d/%.2d/%d " \
  171.                                     "%d:%.2d:%.2d.%.4d",
  172.                                     sysTime.wMonth, sysTime.wDay, 
  173.                                     sysTime.wYear, sysTime.wHour,
  174.                                     sysTime.wMinute, sysTime.wSecond,
  175.                                     sysTime.wMilliseconds );
  176.  
  177.                            TextOut( hDC, 10, 40, szMsg, strlen( szMsg ) );
  178.  
  179.                            CloseHandle( hFile );
  180.                            ReleaseDC( hWnd, hDC );
  181.                         }
  182.                         break;
  183.  
  184.                  case IDM_ABOUT :
  185.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  186.                         break;
  187.  
  188.                  case IDM_EXIT :
  189.                         DestroyWindow( hWnd );
  190.                         break;
  191.               }
  192.               break;
  193.       
  194.       case WM_DESTROY :
  195.               PostQuitMessage(0);
  196.               break;
  197.  
  198.       default :
  199.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  200.    }
  201.  
  202.    return( 0L );
  203. }
  204.  
  205.  
  206. LRESULT CALLBACK About( HWND hDlg,           
  207.                         UINT message,        
  208.                         WPARAM wParam,       
  209.                         LPARAM lParam)
  210. {
  211.    switch (message) 
  212.    {
  213.        case WM_INITDIALOG: 
  214.                return (TRUE);
  215.  
  216.        case WM_COMMAND:                              
  217.                if (   LOWORD(wParam) == IDOK         
  218.                    || LOWORD(wParam) == IDCANCEL)    
  219.                {
  220.                        Endialog(hDlg, TRUE);        
  221.                        return (TRUE);
  222.                }
  223.                break;
  224.    }
  225.  
  226.    return (FALSE); 
  227. }